package net.mfjassociates.financial.expense.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import net.mfjassociates.financial.expense.client.managed.request.ApplicationRequestFactory;
import net.mfjassociates.financial.expense.client.managed.request.ItemDetailProxy;
import net.mfjassociates.financial.expense.client.managed.request.ItemDetailRequest;
import net.mfjassociates.financial.expense.client.managed.request.ReportDetailProxy;
import net.mfjassociates.financial.expense.client.managed.request.TaxCodeProxy;
import net.mfjassociates.financial.expense.client.managed.ui.ItemDetailDetailsView;
import net.mfjassociates.financial.expense.client.managed.ui.ItemDetailEditView;
import net.mfjassociates.financial.expense.client.managed.ui.ItemDetailListView;
import net.mfjassociates.financial.expense.client.managed.ui.ItemDetailMobileDetailsView;
import net.mfjassociates.financial.expense.client.managed.ui.ItemDetailMobileEditView;
import net.mfjassociates.financial.expense.client.scaffold.ScaffoldApp;
import net.mfjassociates.financial.expense.client.scaffold.place.CreateAndEditProxy;
import net.mfjassociates.financial.expense.client.scaffold.place.FindAndEditProxy;
import net.mfjassociates.financial.expense.client.scaffold.place.ProxyPlace;

public class ItemDetailActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public ItemDetailActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new ItemDetailDetailsActivity((EntityProxyId<ItemDetailProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? ItemDetailMobileDetailsView.instance() : ItemDetailDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<net.mfjassociates.financial.expense.client.managed.request.ItemDetailProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<ItemDetailProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        ItemDetailEditView.instance().setCreating(true);
        final ItemDetailRequest request = requests.itemDetailRequest();
        Activity activity = new CreateAndEditProxy<ItemDetailProxy>(ItemDetailProxy.class, request, ScaffoldApp.isMobile() ? ItemDetailMobileEditView.instance() : ItemDetailEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ItemDetailProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new ItemDetailEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ItemDetailMobileEditView.instance() : ItemDetailEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        ItemDetailEditView.instance().setCreating(false);
        EntityProxyId<ItemDetailProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<ItemDetailProxy>(proxyId, requests, ScaffoldApp.isMobile() ? ItemDetailMobileEditView.instance() : ItemDetailEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ItemDetailProxy proxy) {
                ItemDetailRequest request = requests.itemDetailRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new ItemDetailEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ItemDetailMobileEditView.instance() : ItemDetailEditView.instance(), activity, proxyId);
    }
}
